<template>
  <el-header class="navbar">
    <div class="navbar-content">
      <!-- Logo部分 -->
      <div class="logo" @click="$router.push('/')">
        <el-icon class="logo-icon"><Cpu /></el-icon>
        <span class="logo-text">PCB芯选</span>
      </div>
      
      <!-- 导航菜单 -->
      <div class="nav-menu">
        <el-menu
          mode="horizontal"
          background-color="transparent"
          text-color="#ffffff"
          active-text-color="#00D4FF"
          :ellipsis="false"
          :default-active="activeIndex"
          @select="handleMenuSelect"
        >
          <el-menu-item index="home">首页</el-menu-item>
          <el-menu-item index="products">商品中心</el-menu-item>
          <el-menu-item index="about">关于我们</el-menu-item>
        </el-menu>
      </div>
      
      <!-- 右侧操作区 -->
      <div class="user-actions">
        <!-- 未登录状态 -->
        <template v-if="!isLoggedIn">
          <el-button type="primary" @click="$router.push('/login')">登录</el-button>
          <el-button @click="$router.push('/register')">注册</el-button>
        </template>
        
        <!-- 已登录状态 -->
        <template v-else>
          <!-- 购物车 -->
          <el-badge :value="cartItemCount" :hidden="cartItemCount === 0" class="cart-badge">
            <el-button 
              type="info" 
              @click="$router.push('/cart')"
              class="cart-btn"
            >
              <el-icon><ShoppingCart /></el-icon>
              购物车
            </el-button>
          </el-badge>
          
          <!-- 用户菜单 -->
          <div class="custom-user-dropdown" @click="toggleUserDropdown">
            <div class="user-menu-btn">
              <el-icon><User /></el-icon>
              <span class="username">{{ currentUser?.username || '用户' }}</span>
              <el-icon class="dropdown-arrow" :class="{ 'is-reverse': showUserDropdown }">
                <ArrowDown />
              </el-icon>
            </div>
            <div v-show="showUserDropdown" class="user-dropdown-menu" @click.stop>
              <div class="dropdown-item" @click="handleUserCommand('profile')">
                <el-icon><User /></el-icon>
                <span>个人中心</span>
              </div>
              <div class="dropdown-item" @click="handleUserCommand('orders')">
                <el-icon><List /></el-icon>
                <span>我的订单</span>
              </div>
              <div class="dropdown-item" @click="handleUserCommand('cart')">
                <el-icon><ShoppingCart /></el-icon>
                <span>购物车</span>
              </div>
              <div class="dropdown-divider"></div>
              <div class="dropdown-item logout-item" @click="handleUserCommand('logout')">
                <el-icon><SwitchButton /></el-icon>
                <span>退出登录</span>
              </div>
            </div>
          </div>
        </template>
      </div>
    </div>
  </el-header>
</template>

<script>
import { computed, ref, onMounted, onUnmounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Cpu, 
  User, 
  ShoppingCart, 
  ArrowDown, 
  List, 
  SwitchButton 
} from '@element-plus/icons-vue'

export default {
  name: 'Navbar',
  components: {
    Cpu,
    User,
    ShoppingCart,
    ArrowDown,
    List,
    SwitchButton
  },
  setup() {
    const router = useRouter()
    const route = useRoute()
    const store = useStore()
    
    // 响应式数据
    const showUserDropdown = ref(false)
    
    // 计算属性
    const isLoggedIn = computed(() => store.getters.isUserLoggedIn)
    const currentUser = computed(() => store.getters.currentUser)
    const cartItemCount = computed(() => store.getters.cartItemCount)
    
    // 获取当前激活的菜单项
    const activeIndex = computed(() => {
      const path = route.path
      if (path === '/') return 'home'
      if (path.startsWith('/products')) return 'products'
      if (path.startsWith('/about')) return 'about'
      return 'home'
    })
    
    // 处理菜单选择
    const handleMenuSelect = (index) => {
      switch (index) {
        case 'home':
          router.push('/')
          break
        case 'products':
          router.push('/products')
          break
        case 'about':
          router.push('/about')
          break
      }
    }
    
    // 切换用户下拉菜单
    const toggleUserDropdown = () => {
      showUserDropdown.value = !showUserDropdown.value
    }

    // 点击外部关闭下拉菜单
    const handleClickOutside = (event) => {
      const userDropdown = document.querySelector('.custom-user-dropdown')
      if (userDropdown && !userDropdown.contains(event.target)) {
        showUserDropdown.value = false
      }
    }

    // 处理用户菜单命令
    const handleUserCommand = async (command) => {
      showUserDropdown.value = false // 关闭下拉菜单
      switch (command) {
        case 'profile':
          router.push('/profile')
          break
        case 'orders':
          router.push('/orders')
          break
        case 'cart':
          router.push('/cart')
          break
        case 'logout':
          try {
            await ElMessageBox.confirm(
              '确定要退出登录吗？',
              '提示',
              {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
              }
            )
            
            // 执行登出
            store.dispatch('logoutUser')
            ElMessage.success('已退出登录')
            
            // 如果当前页面需要登录，跳转到首页
            const protectedRoutes = ['/profile', '/orders', '/cart']
            if (protectedRoutes.some(route => router.currentRoute.value.path.startsWith(route))) {
              router.push('/')
            }
          } catch {
            // 用户取消登出
          }
          break
      }
    }

    // 生命周期钩子
    onMounted(() => {
      document.addEventListener('click', handleClickOutside)
    })

    onUnmounted(() => {
      document.removeEventListener('click', handleClickOutside)
    })
    
    return {
      isLoggedIn,
      currentUser,
      cartItemCount,
      activeIndex,
      showUserDropdown,
      handleMenuSelect,
      handleUserCommand,
      toggleUserDropdown
    }
  }
}
</script>

<style scoped>
.navbar {
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  height: 70px;
  padding: 0;
  position: relative;
  z-index: 1000;
}

.navbar-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.logo:hover {
  transform: scale(1.05);
}

.logo-icon {
  font-size: 32px;
  color: #00D4FF;
}

.logo-text {
  font-size: 24px;
  font-weight: bold;
  background: linear-gradient(45deg, #00D4FF, #4F46E5);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.nav-menu {
  flex: 1;
  display: flex;
  justify-content: flex-start;
  margin-left: 40px;
}

:deep(.el-menu--horizontal) {
  border-bottom: none !important;
}

:deep(.el-menu--horizontal .el-menu-item) {
  border-bottom: none !important;
}

:deep(.el-menu-item) {
  border-bottom: none !important;
  margin: 0 20px;
  font-weight: 500;
  transition: all 0.3s ease;
}

:deep(.el-menu-item:hover) {
  color: #00D4FF !important;
  background-color: rgba(0, 212, 255, 0.1);
}

:deep(.el-menu-item.is-active) {
  color: #00D4FF !important;
  background-color: rgba(0, 212, 255, 0.1);
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 15px;
}

.cart-badge {
  position: relative;
}

.cart-btn {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(0, 212, 255, 0.3);
  color: #ffffff;
  transition: all 0.3s ease;
}

.cart-btn:hover {
  background: rgba(0, 212, 255, 0.2);
  border-color: #00D4FF;
}

/* 自定义用户下拉菜单 */
.custom-user-dropdown {
  position: relative;
  z-index: 1001;
}

.user-menu-btn {
  background: linear-gradient(135deg, #00D4FF, #4F46E5);
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #ffffff;
  font-size: 14px;
}

.user-menu-btn:hover {
  background: linear-gradient(135deg, #4F46E5, #00D4FF);
  transform: translateY(-1px);
  box-shadow: 0 4px 15px rgba(0, 212, 255, 0.3);
}

.username {
  font-weight: 500;
}

.dropdown-arrow {
  font-size: 14px;
  transition: transform 0.3s ease;
}

.dropdown-arrow.is-reverse {
  transform: rotate(180deg);
}

.user-dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: rgba(26, 26, 46, 0.95);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
  z-index: 9999;
  margin-top: 8px;
  overflow: hidden;
  min-width: 180px;
}

.dropdown-item {
  padding: 12px 16px;
  color: #ffffff;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 10px;
}

.dropdown-item:hover {
  background: rgba(0, 212, 255, 0.2);
  color: #00D4FF;
}

.dropdown-item .el-icon {
  font-size: 16px;
}

.dropdown-divider {
  height: 1px;
  background: rgba(255, 255, 255, 0.1);
  margin: 4px 0;
}

.logout-item:hover {
  background: rgba(255, 75, 75, 0.2);
  color: #ff6b6b;
}



/* 响应式设计 */
@media (max-width: 768px) {
  .navbar-content {
    flex-direction: column;
    height: auto;
    padding: 10px;
  }
  
  .nav-menu {
    margin: 10px 0;
    width: 100%;
  }
  
  :deep(.el-menu--horizontal) {
    justify-content: center;
  }
  
  :deep(.el-menu-item) {
    margin: 0 10px;
  }
  
  .user-actions {
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
  }
}
</style> 